<script lang="ts" setup name="XtxCity">
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
import { ref, watch } from 'vue'
import { AreaList, CityResult } from '@/types/goods'
// import { emit } from 'process'
const active = ref(false)
const closeCity = ref(null)

const emit = defineEmits<{
  (e: 'changeCity', value: CityResult): void
}>()
const props = defineProps<{
  userAddress: String
}>()
onClickOutside(closeCity, () => {
  active.value = false
})
const clickFlag = () => {
  active.value = !active.value
}
const cityListData = ref<AreaList[]>([])
const cacheList = ref<AreaList[]>([])
const getCityList = async () => {
  await axios
    .get<AreaList[]>(
      ' https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json '
    )
    .then((res) => {
      cityListData.value = res.data
      cacheList.value = res.data
    })
  // console.log(cityListData.value)
}
getCityList()

// 选择城市
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: '',
})
const cityOptions = (i: AreaList) => {
  // console.log(i)
  if (i.level === 0) {
    changeResult.value.cityCode = i.code
    changeResult.value.cityName = i.name
    cityListData.value = i.areaList
  }
  if (i.level === 1) {
    changeResult.value.provinceCode = i.code
    changeResult.value.provinceName = i.name
    cityListData.value = i.areaList
  }
  if (i.level === 2) {
    changeResult.value.countyCode = i.code
    changeResult.value.countyName = i.name
    // cityListData.value = i.areaList
    active.value = false
    emit('changeCity', changeResult.value)
  }
}
// 城市值重置
watch(active, (value) => {
  if (!value) {
    cityListData.value = cacheList.value
  }
})
</script>
<template>
  <div class="xtx-city">
    <div class="select" @click="clickFlag" :class="{ active: active }">
      <span class="value" v-if="userAddress">{{ props.userAddress }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active" ref="closeCity">
      <span
        class="ellipsis"
        @click="cityOptions(i)"
        v-for="i in cityListData"
        :key="i.name"
        >{{ i.name }}</span
      >
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
